<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Good;
use App\Shopimage;
use DB;



class GoodsController extends Controller {

    public function goodslist(){
        $goods=Good::paginate(12);//自带分页
        return view('admin\goodslist',['goodslist'=>$goods]);
    }
    

    public function goodsadd(request $req){
        //var_dump($num);
        //for($i=0;$i<$num;$i++){
        //    echo $i;
        //}


        if($req->isMethod('post')){
            $num=count($_FILES['shop_img']['name']);

            //dd($req->file('shop_img'));

          /* $this->validate($req,[
                'goods_name'=>'required',
                'goods_sn'=>'required|integer|numeric',//不能为空,整数,数字
                'market_price'=>'required|numeric',
                'shop_price'=>'required|numeric ',
                'shop_describe'=>'required',
            ],
               [
                   'required'=>'请填写:attribute!',
                   'integer'=>'必须是整数',
                   'numeric'=>'必须是数字',
                ]
           );*/



            //原图文件目录路径是否存在,不存在就自动创建
            $path='./upload/'.date('Ymd/');
            if(!is_dir($path)){
                mkdir($path,0777,true);
            }

            //小图,缩略图文件目录路径是否存在,不存在就自动创建
            $smallpath='./upload/small/'.date('Ymd/');
            if(!is_dir($smallpath)){
                mkdir($smallpath,0777,true);
            }

            //默认提取第一张图片做为缩略图
            $smallurl=$smallpath.$_FILES['shop_img']['name'][0];

            $goodsadd=new Good();
            $goodsadd->goods_name=$req->input('goods_name');
            $goodsadd->goods_sn=$req->input('goods_sn');
            $goodsadd->market_price=$req->input('market_price');
            $goodsadd->shop_price=$req->input('shop_price');
            $goodsadd->shop_describe=$req->input('shop_describe');
            $goodsadd->shop_img=$smallurl;
            $goodsadd->inputtime=date('Y/m/d H:i:s',time());
            //var_dump($req->input());
            $goodsadd->save();

            if($goodsadd->save()){
            echo 'ok';
             $goods_id=DB::table('goods')->max('goods_id');

            for($i=0;$i<$num;$i++){
                $shopimages=new Shopimage();

                $shop_img_url=move_uploaded_file($_FILES['shop_img']['tmp_name'][$i],$path.time().'_'.$_FILES['shop_img']['name'][$i]);
                //原图路径
                $shop_img_path=$path.time().'_'.$_FILES['shop_img']['name'][$i];


                //小图缩略图片路径
                $smallurl=$smallpath.time().'_'.$_FILES['shop_img']['name'][$i];

                //缩略图
                $smimg=$shop_img_path;
                list($bw,$bh)=getimagesize($smimg);
                $big=imagecreatefromjpeg($smimg);//原图
                $small=imagecreatetruecolor($bw/2,$bh/2);//小图
                imagecopyresampled($small,$big,0 ,0 ,0 ,0 ,$bw/2 ,$bh/2 ,$bw ,$bh );
                imagepng($small,$smallurl);

                //$imgdata[] = $smallurl;
                $shopimages->goods_id=$goods_id;
                $shopimages->shopimage = $smallurl;
                $shopimages->shopimage_yt = $shop_img_path;
                $shopimages->shopimage_bh = $i;
                $shopimages->save();

            }
            }else{
             unlink($smallurl);//写入数据库不成功删除小图,缩略图文件

                }
        }


        return view('admin\goodsadd');
    }


    public function goodsedit(request $req,$goods_id){
        if(empty($_POST)){
            $goodf=Good::find($goods_id);
            return view('admin\goodsedit',['goodsfind'=>$goodf]);
        }else{
            $goodedit=Good::find($goods_id);
            $goodedit->goods_name=$req->input('goods_name');
            $goodedit->goods_sn=$req->input('goods_sn');
            $goodedit->market_price=$req->input('market_price');
            $goodedit->shop_price=$req->input('shop_price');
            $goodedit->shop_describe=$req->input('shop_describe');
            //$goodsedit->shop_img=$shop_img_path;
            $goodedit->inputtime=date('Y/m/d H:i:s',time());

            $goodedit->save();
        }


    }

    public function goodsdel($goods_id){
        $gooddel=Good::find($goods_id);
        return $gooddel->delete() ? '删除成功!' : '删除失败!';
        return view('admin\goodsedit');
    }
}
